import { fetchCommon } from "@/utils/fetch";

export const hostName = '818ps.com';

export function ipsApi(apiPath: string) { return '//' + hostName + apiPath; }
//|'omit' | 'same-origin'
const options: {credentials:'include'|'omit'} = {
    credentials: 'include'
};
// 获取风格版式
export const getFormatStyle = () => {
    return fetchCommon('/aidraw/style',options).then(res=>res.json());
}
export interface IGetJobData { 
    promt: string;
    style: string;
    format: string;
    definition: string;
    channel_id: string;
    is_synthesis: string;
    num: number;
    [key: string | number]: string | number;
}
export const getAiDrawJob = (data:IGetJobData) => { 
    const formData = new FormData()
    for (let key in data) { 
        formData.append(key,data[key] as string)
    }
    return fetchCommon('/aidraw/image', {
        method: 'POST',
        body: formData,
        ...options
    }).then(res=>res.json());
}
export const getAiConsumerPoint = () => {
    return fetchCommon('/aidraw/get-num',options).then(res=>res.json());
}
export const checkAiDraw = (jobId:string) => { 
    return fetchCommon(`/aidraw/check?jobId=${jobId}`,options).then(res=>res.json());
}
// 取消ai生成文案的任务
export const closeAiDraw = (jobId:string) => { 
    return fetchCommon(`/aidraw/close?jobId=${jobId}`,options);
}

// 创建绘图区域
export const createDrawChannel = (name:string) => { 
    return fetch(`/aidraw/add-channel`, {
        method: 'POST',
        body: JSON.stringify({name,open:0}),
        ...options
    }).then(res=>res.json());
}
// 获取绘图区域
export const getDrawChannels = () => { 
    return fetchCommon(`/aidraw/get-channel`,options).then(res=>res.json());
}

/// 放大图片--预览使用
//is_synthesis // 绘图类型 0:ai绘图 1:ai商品图 2:ai绘图编辑器 3:放大
export const getHighImgJob = (id:number) => { 
    return fetchCommon(`/aidraw/up-image?id=${id}&is_synthesis=3`, options).then(res => res.json());
}
// 高清大图check
export const checkHighPreview = (jobId:string) => { 
    return fetchCommon(`/aidraw/check-img?jobId=${jobId}`,options).then(res=>res.json());
}

export const getChannelMessgeList = ({ channelId, page = 1, created }: { channelId: number; page: number; created:number}) => { 
    return fetchCommon(`/aidraw/list?channel_id=${channelId}&page=${page}&is_synthesis=2&created=${created}`,options).then(res=>res.json());
}
export const updateMessageStatus = ({ id, status }: { id: string; status: string;}) => { 
    return fetchCommon(`/aidraw/update-status?id=${id}&status=${status}`,options).then(res=>res.json());
}
// 添加到图片资源库
export const uploadToAsset = (id:number) => { 
    return fetchCommon(`/aidraw/user-assert?id=${id}`,options).then(res=>res.json());
}
// 获取图片预设
export const getPresetDes = () => { 
    return fetchCommon('/aidraw/description',options).then(res=>res.json());
}

export const getUserInfo = () => { 
      return fetchCommon('/api/getuserinfo', {
        method: 'POST',
        credentials: 'include'
      }).then(res=>res.json())
}


const isProd = process.env.NODE_ENV === 'production'
const urldomain = isProd?'https://api.818ps.com':''
export const getUserInfoForApp = () => { 
    return fetchCommon(`${urldomain}/v4/user/userInfoAlls?newTimestamp=${new Date().getTime()}`, {credentials:'omit'}).then(res=>res.json())
}
export const getVisitorAuth = () => { 
    return fetchCommon(`${urldomain}/v4/visitor/userInfo`, {credentials:'omit'}).then(res=>res.json())
}
///v4/login/phoneValidCode  验证码登录
export const phoneValidCode = ({ tel, authKey }: { tel: string; authKey: string }) => { 
    const formData = new FormData()
    formData.append('tel',tel)
    formData.append('authKey',authKey)
    return fetchCommon(`${urldomain}/v4/login/phoneValidCode`,{
        method: 'POST',
        body:formData,
        ...{credentials:'omit'}
    } ).then(res=>res.json())
}
export const phoneCodeLogin = ({ telNum, telCode, visitorAuthKey }: { telNum: string; telCode: string;visitorAuthKey:string }) => { 
    const formData = new FormData()
    formData.append('telNum',telNum)
    formData.append('telCode',telCode)
    formData.append('visitorAuthKey',visitorAuthKey)
    return fetchCommon(`${urldomain}/v4/login/phoneLogin`,{
        method: 'POST',
        body:formData,
        ...{credentials:'omit'}
    } ).then(res=>res.json())
}


export const qqLoginApi = ({ accessToken,visitorAuthKey }: { accessToken: string;visitorAuthKey:string }) => { 
    const formData = new FormData()
    formData.append('accessToken',accessToken)
    formData.append('visitorAuthKey',visitorAuthKey)
    return fetchCommon(`${urldomain}/v4/login/qq`,{
        method: 'POST',
        body:formData,
        ...options
    } ).then(res=>res.json())
}



export default () => { }


